<template>
  <el-dialog
    :title="!dataForm.buId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="店名" prop="buName">
      <el-input v-model="dataForm.buName" placeholder="店名"></el-input>
    </el-form-item>
    <el-form-item label="店铺地址" prop="buAddress">
      <el-input v-model="dataForm.buAddress" placeholder="店铺地址"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="buContactDetails">
      <el-input v-model="dataForm.buContactDetails" placeholder="联系方式"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="buCreateDate">
      <el-input v-model="dataForm.buCreateDate" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          buId: 0,
          buName: '',
          buAddress: '',
          buContactDetails: '',
          buCreateDate: ''
        },
        dataRule: {
          buName: [
            { required: true, message: '店名不能为空', trigger: 'blur' }
          ],
          buAddress: [
            { required: true, message: '店铺地址不能为空', trigger: 'blur' }
          ],
          buContactDetails: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          buCreateDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.buId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.buId) {
            this.$http({
              url: this.$http.adornUrl(`/sulingpxqg/business/info/${this.dataForm.buId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.buName = data.business.buName
                this.dataForm.buAddress = data.business.buAddress
                this.dataForm.buContactDetails = data.business.buContactDetails
                this.dataForm.buCreateDate = data.business.buCreateDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sulingpxqg/business/${!this.dataForm.buId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'buId': this.dataForm.buId || undefined,
                'buName': this.dataForm.buName,
                'buAddress': this.dataForm.buAddress,
                'buContactDetails': this.dataForm.buContactDetails,
                'buCreateDate': this.dataForm.buCreateDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
